package langnstats.project.lib.crossvalidation;

import java.util.ArrayList;

public class CrossValidationResult extends ArrayList<CrossValidationResultItem> implements Comparable<CrossValidationResult>{
	private static final long serialVersionUID = 1L;
	private CrossValidationResultItem[] items;
	private String modelDescription;
	
	public String getModelDescription() {
		return modelDescription;
	}

	public CrossValidationResult(String modelDescription, int n){
		this.modelDescription = modelDescription;
		this.items = new CrossValidationResultItem[n];
	}

	public int getN() { return items.length; }
	
	public double getAverageLogLikelihood(){
		double sum = 0;
		int totalTokenCount = 0;
		for(CrossValidationResultItem item : this){
			int tokenCount = item.getTokenCount();
			totalTokenCount += tokenCount;
			sum += tokenCount*item.getAverageLL();
		}
		return sum/totalTokenCount;
	}
	
	public CharSequence makeReport(){
		@SuppressWarnings("unused")
		String newLine = System.getProperty("line.separator");
		StringBuffer report = new StringBuffer();
		report.append('<').append(this.getModelDescription()).append('>');
		report.append("  A-LL("+this.size()+"): ").append(this.getAverageLogLikelihood());
		
		/*
		report.append(newLine);
		for(int i=0; i<this.size(); i++){
			CrossValidationResultItem item = this.get(i);
			report.append("Cross-validation #").append(i+1).append(": ");
			report.append(item.makeReport()).append(newLine);
		}*/
		
		return report;		
	}

	public int compareTo(CrossValidationResult arg0) {
		double diff = this.getAverageLogLikelihood() - arg0.getAverageLogLikelihood();
		return (diff<0)?-1:(diff==0?0:1);
	}
	
}
